package com.dmap.knowledge.controller.background;

import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import com.dmap.knowledge.constants.KnowledgeConstant;
import com.dmap.knowledge.controller.api.param.*;
import com.dmap.knowledge.service.ArticleExtensionService;
import com.dmap.knowledge.service.ArticleHistoryService;
import com.dmap.knowledge.service.ArticleService;
import com.dmap.knowledge.vo.ArticleBackSelectAll;
import com.dmap.knowledge.vo.ArticleInfoOneVo;
import com.dmap.knowledge.vo.ArticleLinkVo;
import com.dmap.knowledge.vo.ArticleLinksVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(value = "文章", description = "background/文章")
@RestController
@RequestMapping(value = "/background/dmap-knowledge-server/v1/article")
public class ArticleBackController {
    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleHistoryService articleHistoryService;
    @Resource
    private ArticleExtensionService articleExtensionService;
    
    @ResponseBody
    @ApiOperation(value = "新增文章", notes = "新增文章")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public BaseVo insertArticle(@RequestBody ArticleParam articleParam) {
        BaseVo result = new BaseVo();
        if (articleParam.getLang() == null || articleParam.getAppList() == null || articleParam.getCoverUrl() == null ||
                articleParam.getArticleUrl() == null || articleParam.getSummary() == null ||
                articleParam.getTitle() == null || articleParam.getAuthorName() == null ||
                articleParam.getStartTime() == 0 || articleParam.getEndTime() == 0 || articleParam.getTags() == null || articleParam.getUploaderId() == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (!StringUtils.isNumeric(articleParam.getUploaderId()) || StringUtils.isBlank(articleParam.getLang()) || StringUtils.isBlank(articleParam.getCoverUrl()) ||
                StringUtils.isBlank(articleParam.getArticleUrl()) || StringUtils.isBlank(articleParam.getSummary()) ||
                StringUtils.isBlank(articleParam.getTitle()) || StringUtils.isBlank(articleParam.getAuthorName())
                ) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = articleService.insertArticle(articleParam);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
        }
        return result;
    }
    
    @ResponseBody
    @ApiOperation(value = "修改文章", notes = "修改文章")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseVo updateArticleDetail(@RequestBody ArticleEditParam articleEditParam) {
        BaseVo result = new BaseVo();
        if (articleEditParam.getArticleId() == null || articleEditParam.getLang() == null || articleEditParam.getApp() == null || articleEditParam.getCoverUrl() == null || articleEditParam.getArticleUrl() == null || articleEditParam.getSummary() == null ||
                articleEditParam.getTitle() == null ||
                articleEditParam.getStartTime() == 0 || articleEditParam.getEndTime() == 0 || articleEditParam.getTags() == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (!StringUtils.isNumeric(articleEditParam.getArticleId()) || StringUtils.isBlank(articleEditParam.getLang()) || StringUtils.isBlank(articleEditParam.getCoverUrl()) || StringUtils.isBlank(articleEditParam.getArticleUrl()) || StringUtils.isBlank(articleEditParam.getSummary()) ||
                StringUtils.isBlank(articleEditParam.getTitle())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = articleService.updateArticle(articleEditParam);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        return result;
    }
    
    @ResponseBody
    @ApiOperation(value = "发布文章", notes = "发布文章")
    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    public BaseVo updateArticle(@RequestBody ArticlePublishParam articlePublishParam) {
        BaseVo result = new BaseVo();
        String articleId = articlePublishParam.getArticleId();
        String promulgatorId = articlePublishParam.getPromulgatorId();
        if (articleId == null || promulgatorId == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (!StringUtils.isNumeric(articleId) || !StringUtils.isNumeric(promulgatorId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = articleService.updateArticlePublish(articleId, promulgatorId);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        return result;
    }
    
    @ResponseBody
    @ApiOperation(value = "审核文章", notes = "审核文章")
    @RequestMapping(value = "/audit", method = RequestMethod.POST)
    public BaseVo updateArticle(@RequestBody ArticleAuditParam articleAuditParam) {
        BaseVo result = new BaseVo();
        String articleId = articleAuditParam.getArticleId();
        if (articleId == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (!StringUtils.isNumeric(articleId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = articleService.updateArticleAudit(articleId);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        return result;
    }
    
    @Transactional
    @ResponseBody
    @ApiOperation(value = "删除文章", notes = "删除文章")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public BaseVo deleteArticle(@RequestParam String articleIds) {
        BaseVo result = new BaseVo();
        if (articleIds == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(articleIds)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = articleService.deleteArticleList(articleIds);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
            
        }
        return result;
        
    }
    
    @ResponseBody
    @ApiOperation(value = "查询文章", notes = "查询文章")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public BaseVo selectArticle(@RequestParam(required = false) String companyNumber,
                                @RequestParam(required = false) String appNumber,
                                @RequestParam String lang,
                                @RequestParam(required = false) String columnId,
                                @RequestParam(required = false) String key,
                                @RequestParam(required = false, defaultValue = "0") long order,
                                @RequestParam int pageIndex,
                                @RequestParam int pageSize) {
        BaseVo result = new BaseVo();
        if (lang == null || pageIndex == 0 || pageSize == 0) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (appNumber != null && StringUtils.isBlank(appNumber)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (companyNumber != null && StringUtils.isBlank(companyNumber)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (columnId != null && !StringUtils.isNumeric(columnId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(lang)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        ArticleBackSelectAll articleBackSelectAll = new ArticleBackSelectAll();
        articleBackSelectAll.setArticleList(articleExtensionService.selectArticleList(companyNumber, appNumber, lang, columnId, key, order, pageIndex, pageSize));
        articleBackSelectAll.setCount(articleExtensionService.countArticleList(companyNumber, appNumber, lang, columnId, key, order));
        result.setData(articleBackSelectAll);
        return result;
    }
    
    
    /**
     * 查看文章信息
     */
    @ResponseBody
    @ApiOperation(value = "查看文章信息", notes = "查看文章信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public BaseVo selectArticle(@RequestParam String articleId) {
        BaseVo result = new BaseVo();
        if (articleId == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        //做非数字检测
        if (!StringUtils.isNumeric(articleId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        ArticleInfoOneVo articleInfoOneVo = articleService.selectArticleInfo(articleId);
        if (articleInfoOneVo == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        result.setData(articleInfoOneVo);
        return result;
    }
    
    @ResponseBody
    @ApiOperation(value = "选择文章内部跳转", notes = "选择文章内部跳转")
    @RequestMapping(value = "/links", method = RequestMethod.GET)
    public BaseVo selectArticleLinks(@RequestParam(required = false) String companyNumber,
                                @RequestParam(required = false) String appNumber,
                                @RequestParam String lang,
                                @RequestParam(required = false) String columnId,
                                @RequestParam(required = false, defaultValue = "1") int database,
                                @RequestParam int pageIndex,
                                @RequestParam int pageSize) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(lang)|| pageIndex == 0 || pageSize == 0) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (appNumber != null && StringUtils.isBlank(appNumber)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isNotBlank(columnId) && !StringUtils.isNumeric(columnId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
    
        if (StringUtils.isBlank(companyNumber) && StringUtils.isBlank(appNumber)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        ArticleLinksVo articleLinksVo = new ArticleLinksVo();
        List<ArticleLinkVo> articleLinkVoList = articleExtensionService.selectArticleLinkList(companyNumber, appNumber, lang, columnId, database, pageIndex, pageSize);
        articleLinksVo.setArticleLinksVoList(articleLinkVoList);
        articleLinksVo.setCount(articleExtensionService.countArticleLinkList(companyNumber, appNumber, lang, columnId, database));
        result.setData(articleLinksVo);
        return result;
    }
    @ResponseBody
    @ApiOperation(value = "增加文章历史", notes = "增加文章历史")
    @RequestMapping(value = "/insert/history", method = RequestMethod.POST)
    public BaseVo insertArticleHistory(@RequestBody ArticleHistoryParam articleHistoryParam) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(articleHistoryParam.getArticleId()) || StringUtils.isBlank(articleHistoryParam.getUserId()) || !StringUtils.isNumeric(articleHistoryParam.getArticleId()) || !StringUtils.isNumeric(articleHistoryParam.getUserId())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = articleHistoryService.insertArticle(articleHistoryParam);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        return result;
    }


    @ResponseBody
    @ApiOperation(value = "更新文章阅读、收藏、点赞数量", notes = "更新文章阅读、收藏、点赞数量")
    @RequestMapping(value = "/mock", method = RequestMethod.POST)
    public BaseVo updateArticleMock(@RequestBody ArticleMockParam articleMockParam) {
        BaseVo result = new BaseVo();
        if(StringUtils.isBlank(articleMockParam.getArticleId()) ||!StringUtils.isNumeric(articleMockParam.getArticleId())){
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = articleExtensionService.updateArticleMock(articleMockParam);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }else if (success == 2){
            result.setCode(KnowledgeConstant.RESPONSE_ARTICLE_IS_NOT_EXIST_CODE);
            result.setMsg(KnowledgeConstant.RESPONSE_ARTICLE_IS_NOT_EXIST_MSG);
        }
        return result;
    }

}
